<template>
<div class="vue-box">
  <div class="c-panel">
    <!-- 参数栏 -->
    <div class="c-title">检索参数</div>
    <el-form @submit.native.prevent class="conditional_filter">
      <sa-item v-model="p.search" type="text" name="模糊搜索" />
      <sa-item v-model="p.category" type="enum" jtype="4" :jv="dims.RESOURCE_CATEGORY" name="资源类别" :clearable="true" />
      <sa-item name="资源类型">
        <template>
          <el-select v-model="p.type" placeholder="资源类型" clearable>
            <el-option v-for="(k, v) in options" :key="v" :label="k" :value="v">
            </el-option>
          </el-select>
        </template>
      </sa-item>
      <sa-item v-model="p.status" type="enum" jtype="4" :jv="dims.YES_OR_NO" name="启用状态" :clearable="true" />
      <div class="c-item">
        <el-button type="primary" icon="el-icon-search" @click="p.current = 1; f5()">查询</el-button>
      </div>
    </el-form>
    <!-- ------------- 快捷按钮 ------------- -->
    <sa-item type="fast-btn" show="add,get,delete,export,reset" />
    <!-- 数据列表 -->
    <el-table ref="data-table" class="data-table" :data="dataList" border>
      <sa-td type="selection" width="45px" />
      <sa-td type="text" name="序号" prop="sort" width="80px" />
      <sa-td type="text" name="简称" prop="segmentName" />
      <sa-td type="text" name="识别码" prop="code" />
      <sa-td type="enum" name="资源类别" prop="category" :jv="dims.RESOURCE_CATEGORY" />
      <sa-td type="enum" name="资源类型" prop="type" :jv="dims.RESOURCE_TYPE" />
      <sa-td name="所属标签" width="240px">
        <template slot-scope="s">
          <div class="stags">
            <el-tag effect="plain" type="primary" v-for="(k, v) in dims.TAGS" :key="v" v-if="getTagFlag(s.row.tags, v)">
              {{ k }}
            </el-tag>
          </div>
        </template>
      </sa-td>
      <sa-td type="text" name="点击量" prop="clickNum" not="0" />
      <sa-td type="text" name="收藏量" prop="collectNum" not="0"  />
      <sa-td type="enum" name="审核状态" prop="auditStatus" :jv="dims.AUDIT_STATUS" />
      <sa-td type="enum" name="启用状态" prop="status" :jv="dims.YES_OR_NO"/>
      <sa-td name="操作" width="160px" fixed="right">
        <template slot-scope="s">
          <el-button type="text" @click="addHot(s.row)">
            热门
          </el-button>
          <el-button type="text" @click="update(s.row)">
            变更
          </el-button>
          <el-button type="text" @click="del(s.row)">
            <span style="color: red;">
              删除
            </span>
          </el-button>
        </template>
      </sa-td>
    </el-table>
    <!-- 分页 -->
    <div v-if="dataCount">
      <sa-item type="page" :curr.sync="p.current" :size.sync="p.size" :total="dataCount" @change="f5()" />
    </div>
  </div>
</div>
</template>

<script>
import optionsFunc  from '@/mixins/resource-options';
export default {
  name: 'rsr-list',
  data() {
    return {
      // 查询参数
      p: {
        current: 1,
        size: 10,
        search: null,
        status: null,
        type: null,
        category: null,
      },
      dataCount: null,
      // 数据集合
      dataList: [],
      // 字典
      dims: sa.$sys.getCurd(),
      options: null,
      tagsList: [],
    }
  },
  mixins:[optionsFunc],
  created: function () {
    this.f5();
    sa.onInputEnter();
  },
  watch: {
    'p.category': {
      handler(n) {
        if (n) {
          this.p.type=null
          this.options=null
          this.getOptions(n)
        }
      },
      immediate: true
    },
  },
  methods: {
    // 刷新
    f5: function () {
      sa.ajax('/rsr/getList', sa.removeNull(this.p), function (res) {
        this.dataList = res.data;
        this.dataCount = res.dataCount;
        // 刷新表格高度
        sa.f5TableHeight();
      }.bind(this), {
        type: "post"
      });
    },

    // 添加
    add: function () {
      sa.showModel('添加', () => import('./add'));
    },

    // 删除
    del: function (data) {
      sa.confirm('是否删除，此操作不可撤销', function () {
        sa.ajax('/rsr/delete', {
          id: data.id
        }, function (res) {
          sa.ok(res.msg);
          this.f5();
        }.bind(this), {
          type: 'get'
        })
      }.bind(this));
    },

    // 批量删除
    deleteByIds() {
      // 获取选中元素的id列表
      let selection = this.$refs['data-table'].selection;
      let ids = sa.getArrayField(selection, 'id');
      if (selection.length == 0) {
        return sa.msg('请至少选择一条数据')
      }
      // 提交删除
      sa.confirm('是否批量删除选中数据？此操作不可撤销', function () {
        sa.ajax('/rsr/deleteByIds', {
          ids: ids
        }, function (res) {
          sa.ok(res.msg);
          this.f5()
        }.bind(this), {
          type: "post"
        })
      }.bind(this));
    },

    // 变更
    update: function (data) {
      sa.showModel('变更', () => import('./update'), data);
    },

    // 查看 - 根据选中的
    getBySelect: function (_data) {
      var selection = this.$refs['data-table'].selection;
      if (selection.length === 0) {
        return sa.msg('请至少选择一条数据')
      }
      this.getInfo(selection[0]);
    },

    // 查看详情
    getInfo: function (data) {
      sa.showModel('详情', () => import('./info'), data);
    },

    // 处理标签
    getTagFlag(tags, id) {
      let flag = false
      if (tags) {
        tags = JSON.parse(tags)
        if (tags.includes(id)) {
          return true
        }
      }
      return flag
    },
    // 热门
    addHot: function (data) {
      sa.ajax('/rsr/hot/add', {
        resourceId: data.id
      }, function (res) {
        sa.ok(res.msg);
        this.f5()
      }.bind(this), {
        type: 'post'
      })
    },
  },
}
</script>

<style scoped>
.stags {
  display: flex;
  justify-content: start;

  span {
    margin-left: 2px;
  }
}
</style>
